package core.shiro;

import com.jfinal.log.Log;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * Created by Administrator on 2017/3/3.
 */
public class ShiroDbRealm extends AuthorizingRealm{
    /**
     * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
    /**
     * 认证回调函数,登录时调用.返回数据库里获取用户信息
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        Log.getLog(getClass()).info("开始认证");
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        if(!"qaz".equals(token.getUsername())){
            return null;//返回空说明账户不存在
        }

        return new SimpleAuthenticationInfo("qaz","123", getName());
    }
}
